<template>
  <div class="notice-detail">
    <el-card class="notice-card">
      <template #header>
        <div class="card-header">
          <h2>{{ notice.noticeTitle }}</h2>
        </div>
      </template>
      <div v-html="notice.noticeContent" class="notice-content"></div>
      <div class="notice-info">
        <p><strong>发布管理员:</strong> {{ notice.noticeAdminId }}</p>
        <p><strong>创建时间:</strong> {{ formatDate(notice.createTime) }}</p>
        <p><strong>更新时间:</strong> {{ formatDate(notice.updateTime) }}</p>
      </div>
      <el-button type="primary" @click="goBack">返回</el-button>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import http from '../utils/axios';

const route = useRoute();
const router = useRouter();
const notice = ref({});

const fetchNotice = async () => {
  try {
    const response = await http.get(`/notice/${route.params.noticeId}`);
    notice.value = response.data;
  } catch (error) {
    console.error('获取公告详情时发生异常', error);
  }
};

const formatDate = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  });
};

const goBack = () => {
  router.push('/notices');
};

onMounted(() => {
  fetchNotice();
});
</script>

<style scoped>
.notice-detail {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;

}

.notice-card {
  padding: 20px;
}

.card-header {
  text-align: center;
  margin-bottom: 20px;
}

.notice-content {
  margin-bottom: 20px;
  line-height: 1.6;
}

.notice-info {
  margin-bottom: 20px;
}

.notice-info p {
  margin-bottom: 10px;
}

.el-button {
  width: 100%;
  margin-top: 20px;
}
</style>
